home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / gcc / gcc2.7.0 / gcc270cp.lha / gnu / lib / g++-include / std / bastring.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-28  |  18.2 KB  |  570 lines

  1. // Main templates for the -*- C++ -*- string classes.
  2. // Copyright (C) 1994, 1995 Free Software Foundation
  3.  
  4. // This file is part of the GNU ANSI C++ Library.  This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 2, or (at your option)
  8. // any later version.
  9.  
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14.  
  15. // You should have received a copy of the GNU General Public License
  16. // along with this library; see the file COPYING.  If not, write to the Free
  17. // Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  18.  
  19. // As a special exception, if you link this library with files
  20. // compiled with a GNU compiler to produce an executable, this does not cause
  21. // the resulting executable to be covered by the GNU General Public License.
  22. // This exception does not however invalidate any other reasons why
  23. // the executable file might be covered by the GNU General Public License.
  24.  
  25. // Written by Jason Merrill based upon the specification by Takanori Adachi
  26. // in ANSI X3J16/94-0013R2.
  27.  
  28. #ifdef __GNUG__
  29. #pragma interface
  30. #endif
  31.  
  32. #include <std/stddef.h>
  33. #include <std/straits.h>
  34.  
  35. #if _G_USE_EXCEPTIONS
  36.  
  37. #include <stdexcept>
  38. #define OUTOFRANGE(cond) \
  39.   do { if (!(cond)) throw out_of_range (#cond); } while (0)
  40. #define LENGTHERROR(cond) \
  41.   do { if (!(cond)) throw length_error (#cond); } while (0)
  42.  
  43. #else
  44.  
  45. #include <cassert>
  46. #define OUTOFRANGE(cond) assert (!(cond))
  47. #define LENGTHERROR(cond) assert (!(cond))
  48.  
  49. #endif
  50.  
  51. class istream; class ostream;
  52.  
  53. // Should be a nested class basic_string<charT, traits>::Rep, but nested
  54. // classes don't work well with templates in g++.
  55. template <class charT, class traits = string_char_traits<charT> >
  56. struct __bsrep {
  57.   typedef __bsrep Rep;
  58.  
  59.   size_t len, res;
  60.   unsigned char ref;
  61.   bool selfish;
  62.  
  63.   charT* data () { return reinterpret_cast<charT *>(this + 1); }
  64.   charT& operator[] (size_t s) { return data () [s]; }
  65.   charT* grab () { if (selfish) return clone (); ++ref; return data (); }
  66.   void release () { if (--ref == 0) delete this; }
  67.  
  68.   inline static void * operator new (size_t, size_t);
  69.   inline static Rep* create (size_t);
  70.   charT* clone ();
  71.  
  72.   inline void copy (size_t, const charT *, size_t);
  73.   inline void move (size_t, const charT *, size_t);
  74.   inline void set  (size_t, const charT,   size_t);
  75.  
  76. #if _G_ALLOC_CONTROL
  77.   // These function pointers allow you to modify the allocation policy used
  78.   // by the string classes.  By default they expand by powers of two, but
  79.   // this may be excessive for space-critical applications.
  80.  
  81.   // Returns true if ALLOCATED is too much larger than LENGTH
  82.   static bool (*excess_slop) (size_t length, size_t allocated);
  83.   inline static bool default_excess (size_t, size_t);
  84.  
  85.   // Returns a good amount of space to allocate for a string of length LENGTH
  86.   static size_t (*frob_size) (size_t length);
  87.   inline static size_t default_frob (size_t);
  88. #else
  89.   inline static bool excess_slop (size_t, size_t);
  90.   inline static size_t frob_size (size_t);
  91. #endif
  92.  
  93. private:
  94.   Rep &operator= (const Rep &);
  95. };
  96.  
  97. // #include <iterator.h>
  98.  
  99. template <class charT, class traits = string_char_traits<charT> >
  100. class basic_string
  101. {
  102. private:
  103.   typedef __bsrep<charT, traits> Rep;
  104.  
  105. public:
  106. // types:
  107.   typedef traits traits_type;
  108.   typedef charT value_type;
  109.   typedef size_t size_type;
  110.   typedef ptrdiff_t difference_type;
  111.   typedef charT& reference;
  112.   typedef const charT& const_reference;
  113.   typedef charT* pointer;
  114.   typedef const charT* const_pointer;
  115.   typedef pointer iterator;
  116.   typedef const_pointer const_iterator;
  117. #if 0
  118.   typedef reverse_iterator<iterator, value_type,
  119.                            reference, difference_type> reverse_iterator;
  120.   typedef reverse_iterator<const_iterator, value_type, const_reference,
  121.                            difference_type> const_reverse_iterator;
  122. #endif
  123.   static const size_type npos = static_cast<size_type>(-1);
  124.  
  125. private:
  126.   Rep *rep () const { return reinterpret_cast<Rep *>(dat) - 1; }
  127.   void repup (Rep *p) { rep ()->release (); dat = p->data (); }
  128.  
  129. public:
  130.   const charT* data () const
  131.     { return rep ()->data(); }
  132.   size_type length () const
  133.     { return rep ()->len; }
  134.   size_type size () const
  135.     { return rep ()->len; }
  136.   size_type capacity () const
  137.     { return rep ()->res; }
  138.   size_type max_size () const
  139.     { return npos - 1; }        // XXX
  140.   bool empty () const
  141.     { return size () == 0; }
  142.  
  143. // _lib.string.cons_ construct/copy/destroy:
  144.   basic_string& operator= (const basic_string& str)
  145.     {
  146.       if (&str != this) { rep ()->release (); dat = str.rep ()->grab (); }
  147.       return *this;
  148.     }
  149.  
  150.   explicit basic_string (): dat (nilRep.grab ()) { }
  151.   basic_string (const basic_string& str): dat (str.rep ()->grab ()) { }
  152.   basic_string (const basic_string& str, size_type pos, size_type n = npos)
  153.     : dat (nilRep.grab ()) { assign (str, pos, n); }
  154.   basic_string (const charT* s, size_type n)
  155.     : dat (nilRep.grab ()) { assign (s, n); }
  156.   basic_string (const charT* s)
  157.     : dat (nilRep.grab ()) { assign (s); }
  158.   basic_string (size_type n, charT c)
  159.     : dat (nilRep.grab ()) { assign (n, c); }
  160. #if 0
  161.   template<class InputIterator>
  162.     basic_string(InputIterator begin, InputIterator end,
  163.          Allocator& = Allocator());
  164. #endif
  165.  
  166.   ~basic_string ()
  167.     { rep ()->release (); }
  168.  
  169.   void swap (basic_string &s) { charT *d = dat; dat = s.dat; s.dat = d; }
  170.  
  171.   basic_string& append (const basic_string& str, size_type pos = 0,
  172.             size_type n = npos)
  173.     { return replace (length (), 0, str, pos, n); }
  174.   basic_string& append (const charT* s, size_type n)
  175.     { return replace (length (), 0, s, n); }
  176.   basic_string& append (const charT* s)
  177.     { return append (s, traits::length (s)); }
  178.   basic_string& append (size_type n, charT c)
  179.     { return replace (length (), 0, n, c); }
  180. #if 0
  181.   template<class InputIterator>
  182.     basic_string& append(InputIterator first, InputIterator last);
  183. #endif
  184.  
  185.   basic_string& assign (const basic_string& str, size_type pos = 0,
  186.             size_type n = npos)
  187.     { return replace (0, npos, str, pos, n); }
  188.   basic_string& assign (const charT* s, size_type n)
  189.     { return replace (0, npos, s, n); }
  190.   basic_string& assign (const charT* s)
  191.     { return assign (s, traits::length (s)); }
  192.   basic_string& assign (size_type n, charT c)
  193.     { return replace (0, npos, n, c); }
  194. #if 0
  195.   template<class InputIterator>
  196.     basic_string& assign(InputIterator first, InputIterator last);
  197. #endif
  198.  
  199.   basic_string& operator= (const charT* s)
  200.     { return assign (s); }
  201.   basic_string& operator= (charT c)
  202.     { return assign (1, c); }
  203.  
  204.   basic_string& operator+= (const basic_string& rhs)
  205.     { return append (rhs); }
  206.   basic_string& operator+= (const charT* s)
  207.     { return append (s); }
  208.   basic_string& operator+= (charT c)
  209.     { return append (1, c); }
  210.  
  211.   basic_string& insert (size_type pos1, const basic_string& str,
  212.             size_type pos2 = 0, size_type n = npos)
  213.     { return replace (pos1, 0, str, pos2, n); }
  214.   basic_string& insert (size_type pos, const charT* s, size_type n)
  215.     { return replace (pos, 0, s, n); }
  216.   basic_string& insert (size_type pos, const charT* s)
  217.     { return insert (pos, s, traits::length (s)); }
  218.   basic_string& insert (size_type pos, size_type n, charT c)
  219.     { return replace (pos, 0, n, c); }
  220.   iterator insert(iterator p, charT c)
  221.     { insert (p - begin (), 1, c); return p; }
  222.   iterator insert(iterator p, size_type n, charT c)
  223.     { insert (p - begin (), n, c); return p; }
  224. #if 0
  225.   template<class InputIterator>
  226.     void insert(iterator p, InputIterator first, InputIterator last);
  227. #endif
  228.  
  229.   basic_string& remove (size_type pos = 0, size_type n = npos)
  230.     { return replace (pos, n, 0, '\0'); }
  231.   basic_string& remove (iterator pos)
  232.     { return replace (pos - begin (), 1, 0, '\0'); }
  233.   basic_string& remove (iterator first, iterator last)
  234.     { return replace (first - begin (), last - first, 0, '\0'); }
  235.  
  236.   basic_string& replace (size_type pos1, size_type n1, const basic_string& str,
  237.              size_type pos2 = 0, size_type n2 = npos);
  238.   basic_string& replace (size_type pos, size_type n1, const charT* s,
  239.              size_type n2);
  240.   basic_string& replace (size_type pos, size_type n1, const charT* s)
  241.     { return replace (pos, n1, s, traits::length (s)); }
  242.   // non-ANSI:
  243.   basic_string& replace (size_type pos, size_type n1, size_type n2, charT c);
  244.   basic_string& replace (size_type pos, size_type n, charT c)
  245.     { return replace (pos, n, n, c); }
  246.   basic_string& replace (iterator i1, iterator i2, const basic_string& str)
  247.     { return replace (i1 - begin (), i2 - i1, str); }
  248.   basic_string& replace (iterator i1, iterator i2, const charT* s, size_type n)
  249.     { return replace (i1 - begin (), i2 - i1, s, n); }
  250.   basic_string& replace (iterator i1, iterator i2, const charT* s)
  251.     { return replace (i1 - begin (), i2 - i1, s); }
  252.   basic_string& replace (iterator i1, iterator i2, size_type n, charT c)
  253.     { return replace (i1 - begin (), i2 - i1, n, c); }
  254. #if 0
  255.   template<class InputIterator>
  256.     basic_string& replace(iterator i1, iterator i2,
  257.               InputIterator j1, InputIterator j2);
  258. #endif
  259.  
  260. private:
  261.   static charT eos () { return traits::eos (); }
  262.   void unique () { if (rep ()->ref > 1) alloc (capacity (), true); }
  263.   void selfish () { unique (); rep ()->selfish = true; }
  264.  
  265. public:
  266.   charT operator[] (size_type pos) const
  267.     {
  268.       if (pos == length ())
  269.     return eos ();
  270.       return data ()[pos];
  271.     }
  272.  
  273.   reference operator[] (size_type pos)
  274.     { unique (); return (*rep ())[pos]; }
  275.  
  276.   reference at (size_type pos)
  277.     {
  278.       OUTOFRANGE (pos >= length ());
  279.       return (*this)[pos];
  280.     }
  281.   const_reference at (size_type pos) const
  282.     {
  283.       OUTOFRANGE (pos >= length ());
  284.       return data ()[pos];
  285.     }
  286.  
  287. private:
  288.   void terminate () const
  289.     {
  290.       if (capacity () < length () + 1)
  291.     alloc (length () + 1, true);
  292.       traits::assign ((*rep ())[length ()], eos ());
  293.     }
  294.  
  295. public:
  296.   const charT* c_str () const
  297.     { terminate (); return data (); }
  298.   void resize (size_type n, charT c);
  299.   void resize (size_type n)
  300.     { resize (n, eos ()); }
  301.   void reserve (size_type) { }
  302.  
  303.   size_type copy (charT* s, size_type n, size_type pos = 0);
  304.  
  305.   size_type find (const basic_string& str, size_type pos = 0) const
  306.     { return find (str.data(), pos, str.length()); }
  307.   size_type find (const charT* s, size_type pos, size_type n) const;
  308.   size_type find (const charT* s, size_type pos = 0) const
  309.     { return find (s, pos, traits::length (s)); }
  310.   size_type find (charT c, size_type pos = 0) const;
  311.  
  312.   size_type rfind (const basic_string& str, size_type pos = npos) const
  313.     { return rfind (str.data(), pos, str.length()); }
  314.   size_type rfind (const charT* s, size_type pos, size_type n) const;
  315.   size_type rfind (const charT* s, size_type pos = npos) const
  316.     { return rfind (s, pos, traits::length (s)); }
  317.   size_type rfind (charT c, size_type pos = npos) const;
  318.  
  319.   size_type find_first_of (const basic_string& str, size_type pos = 0) const
  320.     { return find_first_of (str.data(), pos, str.length()); }
  321.   size_type find_first_of (const charT* s, size_type pos, size_type n) const;
  322.   size_type find_first_of (const charT* s, size_type pos = 0) const
  323.     { return find_first_of (s, pos, traits::length (s)); }
  324.   size_type find_first_of (charT c, size_type pos = 0) const
  325.     { return find (c, pos); }
  326.  
  327.   size_type find_last_of (const basic_string& str, size_type pos = npos) const
  328.     { return find_last_of (str.data(), pos, str.length()); }
  329.   size_type find_last_of (const charT* s, size_type pos, size_type n) const;
  330.   size_type find_last_of (const charT* s, size_type pos = npos) const
  331.     { return find_last_of (s, pos, traits::length (s)); }
  332.   size_type find_last_of (charT c, size_type pos = npos) const
  333.     { return rfind (c, pos); }
  334.  
  335.   size_type find_first_not_of (const basic_string& str, size_type pos = 0) const
  336.     { return find_first_not_of (str.data(), pos, str.length()); }
  337.   size_type find_first_not_of (const charT* s, size_type pos, size_type n) const;
  338.   size_type find_first_not_of (const charT* s, size_type pos = 0) const
  339.     { return find_first_not_of (s, pos, traits::length (s)); }
  340.   size_type find_first_not_of (charT c, size_type pos = 0) const;
  341.  
  342.   size_type find_last_not_of (const basic_string& str, size_type pos = npos) const
  343.     { return find_last_not_of (str.data(), pos, str.length()); }
  344.   size_type find_last_not_of (const charT* s, size_type pos, size_type n) const;
  345.   size_type find_last_not_of (const charT* s, size_type pos = npos) const
  346.     { return find_last_not_of (s, pos, traits::length (s)); }
  347.   size_type find_last_not_of (charT c, size_type pos = npos) const;
  348.  
  349.   basic_string substr (size_type pos = 0, size_type n = npos) const
  350.     { return basic_string (*this, pos, n); }
  351.  
  352.   int compare (const basic_string& str, size_type pos = 0, size_type n = npos) const;
  353.   // There is no 'strncmp' equivalent for charT pointers.
  354.   int compare (const charT* s, size_type pos, size_type n) const;
  355.   int compare (const charT* s, size_type pos = 0) const
  356.     { return compare (s, pos, traits::length (s)); }
  357.  
  358.   iterator begin () { selfish (); return &(*this)[0]; }
  359.   iterator end () { selfish (); return &(*this)[length ()]; }
  360.   const_iterator begin () const { return &(*rep ())[0]; }
  361.   const_iterator end () const { return &(*rep ())[length ()]; }
  362.  
  363. #if 0
  364.   reverse_iterator       rbegin() { return reverse_iterator (end ()); }
  365.   const_reverse_iterator rbegin() const
  366.     { return const_reverse_iterator (end ()); }
  367.   reverse_iterator       rend() { return reverse_iterator (begin ()); }
  368.   const_reverse_iterator rend() const
  369.     { return const reverse_iterator (begin ()); }
  370. #endif
  371.  
  372. private:
  373.   void alloc (size_type size, bool save) const;
  374.   static size_type _find (const charT* ptr, charT c, size_type xpos, size_type len);
  375.   inline bool check_realloc (size_type s) const;
  376.  
  377.   static Rep nilRep;
  378.   charT *dat;
  379. };
  380.  
  381. template <class charT, class traits>
  382. inline basic_string <charT, traits>
  383. operator+ (const basic_string <charT, traits>& lhs,
  384.        const basic_string <charT, traits>& rhs)
  385. {
  386.   basic_string <charT, traits> str (lhs);
  387.   str.append (rhs);
  388.   return str;
  389. }
  390.  
  391. template <class charT, class traits>
  392. inline basic_string <charT, traits>
  393. operator+ (const charT* lhs, const basic_string <charT, traits>& rhs)
  394. {
  395.   basic_string <charT, traits> str (lhs);
  396.   str.append (rhs);
  397.   return str;
  398. }
  399.  
  400. template <class charT, class traits>
  401. inline basic_string <charT, traits>
  402. operator+ (charT lhs, const basic_string <charT, traits>& rhs)
  403. {
  404.   basic_string <charT, traits> str (1, lhs);
  405.   str.append (rhs);
  406.   return str;
  407. }
  408.  
  409. template <class charT, class traits>
  410. inline basic_string <charT, traits>
  411. operator+ (const basic_string <charT, traits>& lhs, const charT* rhs)
  412. {
  413.   basic_string <charT, traits> str (lhs);
  414.   str.append (rhs);
  415.   return str;
  416. }
  417.  
  418. template <class charT, class traits>
  419. inline basic_string <charT, traits>
  420. operator+ (const basic_string <charT, traits>& lhs, charT rhs)
  421. {
  422.   basic_string <charT, traits> str (lhs);
  423.   str.append (1, rhs);
  424.   return str;
  425. }
  426.  
  427. template <class charT, class traits>
  428. inline bool
  429. operator== (const basic_string <charT, traits>& lhs,
  430.         const basic_string <charT, traits>& rhs)
  431. {
  432.   return (lhs.compare (rhs) == 0);
  433. }
  434.  
  435. template <class charT, class traits>
  436. inline bool
  437. operator== (const charT* lhs, const basic_string <charT, traits>& rhs)
  438. {
  439.   return (rhs.compare (lhs) == 0);
  440. }
  441.  
  442. template <class charT, class traits>
  443. inline bool
  444. operator== (const basic_string <charT, traits>& lhs, const charT* rhs)
  445. {
  446.   return (lhs.compare (rhs) == 0);
  447. }
  448.  
  449. template <class charT, class traits>
  450. inline bool
  451. operator!= (const basic_string <charT, traits>& lhs,
  452.         const basic_string <charT, traits>& rhs)
  453. {
  454.   return (lhs.compare (rhs) != 0);
  455. }
  456.  
  457. template <class charT, class traits>
  458. inline bool
  459. operator!= (const charT* lhs, const basic_string <charT, traits>& rhs)
  460. {
  461.   return (rhs.compare (lhs) != 0);
  462. }
  463.  
  464. template <class charT, class traits>
  465. inline bool
  466. operator!= (const basic_string <charT, traits>& lhs, const charT* rhs)
  467. {
  468.   return (lhs.compare (rhs) != 0);
  469. }
  470.  
  471. template <class charT, class traits>
  472. inline bool
  473. operator< (const basic_string <charT, traits>& lhs,
  474.         const basic_string <charT, traits>& rhs)
  475. {
  476.   return (lhs.compare (rhs) < 0);
  477. }
  478.  
  479. template <class charT, class traits>
  480. inline bool
  481. operator< (const charT* lhs, const basic_string <charT, traits>& rhs)
  482. {
  483.   return (rhs.compare (lhs) > 0);
  484. }
  485.  
  486. template <class charT, class traits>
  487. inline bool
  488. operator< (const basic_string <charT, traits>& lhs, const charT* rhs)
  489. {
  490.   return (lhs.compare (rhs) < 0);
  491. }
  492.  
  493. template <class charT, class traits>
  494. inline bool
  495. operator> (const basic_string <charT, traits>& lhs,
  496.         const basic_string <charT, traits>& rhs)
  497. {
  498.   return (lhs.compare (rhs) > 0);
  499. }
  500.  
  501. template <class charT, class traits>
  502. inline bool
  503. operator> (const charT* lhs, const basic_string <charT, traits>& rhs)
  504. {
  505.   return (rhs.compare (lhs) < 0);
  506. }
  507.  
  508. template <class charT, class traits>
  509. inline bool
  510. operator> (const basic_string <charT, traits>& lhs, const charT* rhs)
  511. {
  512.   return (lhs.compare (rhs) > 0);
  513. }
  514.  
  515. template <class charT, class traits>
  516. inline bool
  517. operator<= (const basic_string <charT, traits>& lhs,
  518.         const basic_string <charT, traits>& rhs)
  519. {
  520.   return (lhs.compare (rhs) <= 0);
  521. }
  522.  
  523. template <class charT, class traits>
  524. inline bool
  525. operator<= (const charT* lhs, const basic_string <charT, traits>& rhs)
  526. {
  527.   return (rhs.compare (lhs) >= 0);
  528. }
  529.  
  530. template <class charT, class traits>
  531. inline bool
  532. operator<= (const basic_string <charT, traits>& lhs, const charT* rhs)
  533. {
  534.   return (lhs.compare (rhs) <= 0);
  535. }
  536.  
  537. template <class charT, class traits>
  538. inline bool
  539. operator>= (const basic_string <charT, traits>& lhs,
  540.         const basic_string <charT, traits>& rhs)
  541. {
  542.   return (lhs.compare (rhs) >= 0);
  543. }
  544.  
  545. template <class charT, class traits>
  546. inline bool
  547. operator>= (const charT* lhs, const basic_string <charT, traits>& rhs)
  548. {
  549.   return (rhs.compare (lhs) <= 0);
  550. }
  551.  
  552. template <class charT, class traits>
  553. inline bool
  554. operator>= (const basic_string <charT, traits>& lhs, const charT* rhs)
  555. {
  556.   return (lhs.compare (rhs) >= 0);
  557. }
  558.  
  559. class istream; class ostream;
  560. template <class charT, class traits> istream&
  561. operator>> (istream&, basic_string <charT, traits>&);
  562. template <class charT, class traits> ostream&
  563. operator<< (ostream&, const basic_string <charT, traits>&);
  564. template <class charT, class traits> istream&
  565. getline (istream&, basic_string <charT, traits>&, charT delim = '\n');
  566.  
  567. #if !defined (_G_NO_EXTERN_TEMPLATES)
  568. #include <std/sinst.h>
  569. #endif
  570.